import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * 所有权限通用路由表
 * 如首页和登录页和一些不用权限的公用页面
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: '首页',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }
]

/**
 * 异步挂载的路由
 * 动态需要根据权限加载的路由表
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // 404 page must be placed at the end !!!
  {
    path: '/example2',
    component: Layout,
    redirect: '/example2/table',
    name: 'Example2',
    meta: { title: '权限管理', icon: 'example', role: ['SYSTEMER', 'admin', 'super_editor'] },
    children: [
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/sys/role'),
        meta: { title: '角色管理', icon: 'eye', role: ['SYSTEMER', 'role'] }
      }/*,
      {
        path: 'role1',
        name: 'role1',
        component: () => import('@/views/sys/role'),
        meta: { title: '数据权限', icon: 'tree' }
      },
      {
        path: 'role2',
        name: 'role2',
        component: () => import('@/views/sys/role'),
        meta: { title: '接口权限', icon: 'tree' }
      }*/
    ]
  },
  {
    path: '/example',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    meta: { title: '系统管理', icon: 'example', role: ['SYSTEMER'] },
    children: [
      {
        path: 'dep',
        name: 'dep',
        component: () => import('@/views/sys/dep'),
        meta: { title: '组织架构', icon: 'tree', role: ['SYSTEMER', 'dept'] }
      },
      {
        path: 'users',
        name: 'users',
        component: () => import('@/views/sys/users'),
        meta: { title: '人员管理', icon: 'user', role: ['SYSTEMER', 'admin', 'super_editor'] }
      },
      {
        path: 'menu',
        name: 'menu',
        component: () => import('@/views/sys/menu'),
        meta: { title: '菜单管理', icon: 'table', role: ['SYSTEMER', 'admin', 'super_editor'] }
      },
      {
        path: 'dic',
        name: 'dic',
        component: () => import('@/views/sys/dic'),
        meta: { title: '数据字典', icon: 'tree', role: ['SYSTEMER', 'admin', 'super_editor'] }
      },
      {
        path: 'table',
        name: 'table',
        component: () => import('@/views/sys/table'),
        meta: { title: '表格管理', icon: 'tree', role: ['SYSTEMER', 'admin', 'super_editor'] }
      },
      /* {
        path: 'table1',
        name: 'table1',
        component: () => import('@/views/sys/table'),
        meta: { title: '顶部菜单', icon: 'tree' }
      },*/
      {
        path: 'tenant',
        name: 'tenant',
        component: () => import('@/views/sys/tenant'),
        meta: { title: '租户管理', icon: 'tree', role: ['SYSTEMER', 'admin', 'super_editor'] }
      }/*,
      {
        path: 'table2',
        name: 'table2',
        component: () => import('@/views/sys/table'),
        meta: { title: '应用管理', icon: 'tree' }
      }*/
    ]
  },
  /* {
    path: '/goods3',
    component: Layout,
    redirect: '/goods3/table',
    name: 'goods3',
    meta: { title: '商城管理', icon: 'example' },
    children: [
      {
        path: 'Product',
        name: 'Product',
        component: () => import('@/views/shop/Product'),
        meta: { title: '商品管理', icon: 'table' }
      },
      {
        path: 'ProductType',
        name: 'ProductType',
        component: () => import('@/views/shop/ProductType'),
        meta: { title: '  商品分类', icon: 'table' }
      }
    ]
  },
  {
    path: '/goods2',
    component: Layout,
    redirect: '/goods2/table',
    name: 'goods2',
    meta: { title: '订单管理', icon: 'example' },
    children: [
      {
        path: 'order',
        name: 'order',
        component: () => import('@/views/sys/table'),
        meta: { title: '  订单管理', icon: 'table' }
      },
      {
        path: 'table',
        name: 'table',
        component: () => import('@/views/sys/table'),
        meta: { title: '  客户管理', icon: 'table' }
      },
      {
        path: 'Integral',
        name: 'Integral',
        component: () => import('@/views/sys/table'),
        meta: { title: '  积分管理', icon: 'table' }
      }
    ]
  },
  {
    path: '/example1',
    component: Layout,
    redirect: '/example1/table1',
    // name: 'Example',
    meta: { title: '物业设置', icon: 'example' },
    children: [
      /!* {
        path: 'company',
        name: 'company',
        component: () => import('@/views/sys/company'),
        meta: { title: '物业公司', icon: 'table' }
      },*!/
      {
        path: 'community2',
        name: 'community2',
        component: () => import('@/views/property/community'),
        meta: { title: '小区管理', icon: 'table' }
      },
      {
        path: 'room',
        name: 'room',
        component: () => import('@/views/property/room'),
        meta: { title: '房号管理', icon: 'tree' }
      },
      {
        path: 'owner',
        name: 'owner',
        component: () => import('@/views/property/owner'),
        meta: { title: '业主管理', icon: 'tree' }
      },
      {
        path: 'slide',
        name: 'slide',
        component: () => import('@/views/property/slide'),
        meta: { title: '幻灯片管理', icon: 'tree' }
      },
      {
        path: 'company3',
        name: 'company3',
        component: () => import('@/views/property/company'),
        meta: { title: '缴费管理', icon: 'table' }
      },
      {
        path: 'company4',
        name: 'company4',
        component: () => import('@/views/property/company'),
        meta: { title: '停车费', icon: 'table' }
      }
    ]
  },*/
  { path: '*', redirect: '/404', hidden: true }
]
/**
 * 实例化vue的时候只挂载constantRouter
 * @returns {VueRouter}
 */
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
